<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="用户名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名或手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="handleExport"
          icon="el-icon-download"
          size="mini"
          type="warning"
          >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="name" width="100">
        <template slot-scope="scope">
          <span style="margin-right: 10px">{{ scope.row.name }}</span>
          <i :class="getIcon(scope.row.politicStatus)"></i>
        </template>
      </el-table-column>
      <el-table-column align="center" label="手机号" prop="phone" />
      <el-table-column
        :formatter="sexFormat"
        align="center"
        label="性别"
        prop="sex"
      />
      <el-table-column label="生日" align="center" prop="birth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birth, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="常住地" prop="address" />
      <el-table-column
        :formatter="politicFormat"
        align="center"
        label="政治面貌"
        prop="politicStatus"
      />
      <el-table-column align="center" label="所属党支部" prop="party" />
      <el-table-column align="center" label="所属联合党委" prop="committee" />
      <el-table-column
        align="center"
        label="注册时间"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="800px">
      <el-form :model="form" :rules="rules" label-width="110px" ref="form">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名" prop="name">
              <el-input placeholder="请输入用户名" v-model="form.name" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input placeholder="请输入用户名" v-model="form.phone" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别">
              <el-radio-group v-model="form.sex">
                <el-radio
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  v-for="dict in sexOptions"
                  >{{ dict.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生日" prop="birth">
              <el-date-picker
                clearable
                placeholder="选择收藏总数"
                size="small"
                style="width: 200px"
                type="date"
                v-model="form.birth"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年龄" prop="age">
              <el-input placeholder="请输入年龄" v-model="form.age" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="政治面貌">
              <el-select placeholder="政治面貌" v-model="form.politicStatus">
                <el-option
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                  v-for="dict in politicStatusOptions"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属党支部" prop="party">
              <el-input placeholder="请输入党支部" v-model="form.party" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属联合党委" prop="committee">
              <el-input placeholder="请输入党委" v-model="form.committee" />
            </el-form-item>
          </el-col>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="点赞总数" prop="digNum">-->
          <!--              <el-input placeholder="请输入点赞总数" v-model="form.digNum"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="收藏总数" prop="collectNum">-->
          <!--              <el-input placeholder="请输入收藏总数" v-model="form.collectNum"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="心愿总数" prop="wishNum">-->
          <!--              <el-input placeholder="请输入心愿总数" v-model="form.wishNum"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="活动总数" prop="activityNum">-->
          <!--              <el-input placeholder="请输入活动总数" v-model="form.activityNum"/>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input placeholder="请输入地址" v-model="form.address" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  exportUser,
} from "@/api/system/users";

export default {
  name: "User",
  data() {
    return {
      // 性别状态字典
      sexOptions: [],
      politicStatusOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        phone: undefined,
        // openid: undefined,
        sex: undefined,
        party: undefined,
        // password: undefined,
        committee: undefined,
        age: undefined,
        politicStatus: undefined,
        // digNum: undefined,
        // collectNum: undefined,
        // wishNum: undefined,
        // activityNum: undefined,
        birth: undefined,
        address: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
        phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
        // openid: [
        //   {required: true, message: '用户名不能为空', trigger: 'blur'}
        // ],
        sex: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        party: [{ required: true, message: "党支部不能为空", trigger: "blur" }],
        // password: [
        //   {required: true, message: '党支部不能为空', trigger: 'blur'}
        // ],
        committee: [
          { required: true, message: "党委不能为空", trigger: "blur" },
        ],
        age: [{ required: true, message: "年龄不能为空", trigger: "blur" }],
        politicStatus: [
          { required: true, message: "政治面貌不能为空", trigger: "blur" },
        ],
        birth: [{ required: true, message: "生日不能为空", trigger: "blur" }],
        address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
      },
    };
  },
  computed: {
    getIcon() {
      return function (i) {
        let icon = "";
        switch (i) {
          case 0:
            icon = "el-icon-menu red";
            break;
          case 1:
            icon = "el-icon-menu green";
            break;
          case 2:
            icon = "el-icon-menu yellow";
            break;
          case 3:
            icon = "el-icon-star-on yellow";
            break;
          case 4:
            icon = "el-icon-user-solid";
            break;
          case 5:
            icon = "el-icon-menu black";
            break;
          case 6:
            icon = "el-icon-menu blue";
            break;
          case 7:
            icon = "el-icon-menu pink";
            break;
        }
        return icon;
      };
    },
  },
  created() {
    this.getList();
    this.getDicts("sys_user_sex").then((response) => {
      console.log("user_sex", response.data);
      this.sexOptions = response.data;
    });
    this.getDicts("politic_status").then((response) => {
      this.politicStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },

    politicFormat(row, column) {
      return this.selectDictLabel(this.politicStatusOptions, row.politicStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        phone: undefined,
        openid: undefined,
        sex: undefined,
        party: undefined,
        password: undefined,
        committee: undefined,
        age: undefined,
        politicStatus: "0",
        createTime: undefined,
        digNum: undefined,
        collectNum: undefined,
        wishNum: undefined,
        activityNum: undefined,
        birth: undefined,
        address: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUser(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.form.sex = this.form.sex + "";
        this.form.politicStatus = this.form.politicStatus + "";
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUser(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addUser(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delUser(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportUser(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>

<style scoped>
.red{
  color: red;
}
.green {
  color: green;
}
.yellow {
  color: yellow;
}
.blue {
  color: blue;
}
.pink {
  color: pink;
}
.black {
  counter-reset: #000;
}
</style>
